package com.ruoyi.eservice.service.impl;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.eservice.common.CustomException;
import com.ruoyi.eservice.pojo.entity.Dish;
import com.ruoyi.eservice.service.IDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.eservice.mapper.CategoryMapper;
import com.ruoyi.eservice.pojo.entity.Category;
import com.ruoyi.eservice.service.ICategoryService;

/**
 * 菜品及套餐分类Service业务层处理
 *
 * @author Jackson
 * @date 2022-11-11
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements ICategoryService
{

    public <T> QueryWrapper<T> createSelWrapper(Class<T> type) {
        QueryWrapper<T> queryWrapper = Wrappers.query();
        queryWrapper.eq("del_flag", "0");
        return queryWrapper;
    }
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询菜品及套餐分类
     *
     * @param categoryId 菜品及套餐分类主键
     * @return 菜品及套餐分类
     */
    @Override
    public Category selectCategoryByCategoryId(Long categoryId)
    {
        return categoryMapper.selectById(categoryId);
    }

    /**
     * 查询菜品及套餐分类列表
     *
     * @param category 菜品及套餐分类
     * @return 菜品及套餐分类
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        QueryWrapper<Category> queryWrapper = createSelWrapper(Category.class);
        queryWrapper.lambda()
                .eq(StringUtils.isNotNull(category.getCategoryId()), Category::getCategoryId, category.getCategoryId())
                .eq(StringUtils.isNotNull(category.getType()), Category::getType, category.getType())
                .like(StringUtils.isNotEmpty(category.getName()), Category::getName, category.getName())
                .eq(StringUtils.isNotNull(category.getSort()), Category::getSort, category.getSort())
                .eq(StringUtils.isNotEmpty(category.getStatus()), Category::getStatus, category.getStatus())
        ;
        return categoryMapper.selectList(queryWrapper);
    }

    /**
     * 新增菜品及套餐分类
     *
     * @param category 菜品及套餐分类
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        category.setCreateBy(SecurityUtils.getUsername());
        category.setCreateTime(DateUtils.getNowDate());
        category.setUpdateBy(SecurityUtils.getUsername());
        category.setUpdateTime(DateUtils.getNowDate());
        int rows = categoryMapper.insert(category);
        return rows;
    }

    /**
     * 修改菜品及套餐分类
     *
     * @param category 菜品及套餐分类
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        category.setUpdateBy(SecurityUtils.getUsername());
        category.setUpdateTime(DateUtils.getNowDate());

        return categoryMapper.updateById(category);
    }

    /**
     * 批量删除菜品及套餐分类
     *
     * @param categoryIds 需要删除的菜品及套餐分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCategoryIds(Long[] categoryIds)
    {
        return categoryMapper.deleteBatchIds(Arrays.asList(categoryIds));
    }

    /**
     * 删除菜品及套餐分类信息
     *
     * @param categoryId 菜品及套餐分类主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByCategoryId(Long categoryId)
    {
        return categoryMapper.deleteById(categoryId);
    }

@Autowired
    private IDishService iDishService;

    public void remove(Long id){
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,id);
        int count = iDishService.count(queryWrapper);

        if(count>0) {
        throw new CustomException("当前分类下关联了菜品，不能删除");
        }
        }
    }


